package com.liruo.learn.spring.mvc.validate;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author:liruo
 * @Date:2023-06-10-20:38:46
 * @Desc
 */
public class TestLogValidator implements ConstraintValidator<TestLog, Object> {
    private Object annotationValue;
    @Override
    public void initialize(TestLog testInit) {
        this.annotationValue = testInit.value();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        System.out.printf("annotationValue=%s, value=%s\n",annotationValue, value);

        if("dynamic-message".equals(annotationValue)){
            context.disableDefaultConstraintViolation();
            context.buildConstraintViolationWithTemplate("TestLog-dynamic-message")
                .addConstraintViolation();
            return false;
        }
        return !"false".equals(annotationValue);
    }
}
